<?php

/**
* Entidad grupo.
*/
class Group {
	
	CONST DB_TGROUP = "group";
	CONST DB_TGROUP_ID = "idgroup";
	CONST DB_TGROUP_NAME = "name";
	CONST DB_TGROUP_CREATOR = "creator";
	CONST DB_TGROUP_LUPDATE = "lastupdate";
	CONST DB_TGROUP_CREATED = "created";
	CONST DB_TGROUP_ACTIVE = "active";
	
	CONST DB_COMMENT = "comment";
	CONST DB_COMMENT_AUTOR = "autor";
	CONST DB_COMMENT_IDCOMMENT = "idcomment";
	CONST DB_COMMENT_IDGROUP = "idgroup";
	CONST DB_COMMENT_IDPLACE = "idplace";
	CONST DB_COMMENT_LUPDATE = "lastupdate";
	CONST DB_COMMENT_CREATE = "create";
	CONST DB_COMMENT_COMMENT = "comment";
	
	/**
	 * Identificador unívoco del grupo.
	 * @var integer
	 * */
	private $id = -1;
	
	/**
	 * Nombre del grupo.
	 * @var string
	 * */
	private $name;
	
	/**
	 * Propietario del grupo.
	 * @var User
	 * */
	private $owner;
	
	/**
	 * Fecha de creación.
	 * @var int
	 * */
	private $created;
	
	/**
	 * Usuarios invitados al grupo
	 * @var User[]
	 * */
	private $users;
	
	private $lastupdate;
	
	private $comments;
	
	private $places;
	
	/**
	 * Inicializa los valores del grupo.
	 * @param string $name Nombre para el grupo.
	 * @param User $owner
	 * */
	function create($name, &$owner){
		$this->name = $name;
		$this->owner = $owner;
		
		$this->created = time();
		
		$this->users = array($owner);
		
		$this->saveToDB();
	}
	
	/**
	 * Obtener el identificador
	 * @return Identificador del grupo
	 * */
	public function getId(){
		return $this->id;
	}
	
	/**
	 * Obtener el nombre del grupo.
	 * @return string El nombre del grupo.
	 * */
	public function getName(){
		return $this->name;
	}
	
	/**
	 * Devuelve el propietario del grupo.
	 * @return User El propietario del grupo.
	 * */
	public function getOwner(){
		return $this->owner;
	}
	
	public function putId($id){
		$this->id = $id;
	}
	
	public function putName($name){
		$this->name = $name;
	}
	
	public function putLastUpdate($lu){
		$this->lastupdate = $lu;
	}
	
	public function putOwner($owner){
		$this->owner = $owner;
	}
	
	public function __contruct(){
		$this->users = array();
	}
	
	/**
	 * Cambia el nombre del grupo y lo guarda.
	 * */
	public function changeName($newGroupName){
		if($this->id == -1) return;
		
		$this->name = formsql($newGroupName);
		$q = "UPDATE `".self::DB_TGROUP."` SET ".self::DB_TGROUP_NAME." = '".$this->name."' WHERE ".self::DB_TGROUP_ID." = ".$this->id;
		sendsql($q);
	}
	
	/**
	 * Añade un destino al grupo.
	 * */
	public function addPlace($place){
		if($this->id == -1) return;
		
		//TODO
	}
	
	/**
	 * Carga un grupo de la base de datos.
	 * @param integer $groupId Identificador unívoco del grupo.
	 * @param bool $active=true Fuerza si sólo se desea obtener aquellos grupos marcados como activos.
	 * @return True en caso de que haya cargado el grupo, false en caso contrario.
	 * */
	public function loadFromDB($groupId, $active=true){
		$active = ($active)? 1:0;
		$q = "SELECT * FROM `".self::DB_TGROUP."` WHERE ".self::DB_TGROUP_ID." = ".$groupId." AND ".self::DB_TGROUP_ACTIVE." = ".$active;
		$res = sendsql($q);
		
		if(empty($res)) return false;
		$this->id = $groupId;
		$owner = new User();
		$owner->load($res[0][self::DB_TGROUP_CREATOR]);
		$this->name = $res[0][self::DB_TGROUP_NAME];
		$this->owner = $owner;
		$this->created = $res[0][self::DB_TGROUP_CREATED];
		$this->lastupdate = $res[0][self::DB_TGROUP_LUPDATE];
		
		return true;
	}
	
	/**
	 * Almacena el grupo en la base de datos.
	 * */
	public function saveToDB(){
		if($this->id != -1) return;
		$name = $this->name;
		$this->created = $this->lastupdate = time();
		$ownerId = $this->owner->getId();
		$q = "INSERT INTO `".self::DB_TGROUP."` (".self::DB_TGROUP_NAME." , ".self::DB_TGROUP_CREATOR." , ".self::DB_TGROUP_CREATED.",".self::DB_TGROUP_LUPDATE.") VALUES ('".$name."' , ".$ownerId.", ".$this->created.",".$this->created.")";
		$res = sendsql($q, true);
		$this->id = $res['id'];
	}
	
	/**
	 * Borra un grupo de la base de datos.
	 * */
	public function deleteFromDB(){
		$q = "UPDATE `".self::DB_TGROUP."` SET ".self::DB_TGROUP_ACTIVE." = 0 WHERE ".self::DB_TGROUP_ID." = ".$this->id;
		sendsql($q);
	}
	
	/**
	 * Cargar usuarios invitados al grupo.
	 * */
	public function loadInvitedUsers($idGroup){
		$q = "SELECT u.*, ug.go FROM ".User::DB_TUSER." u LEFT JOIN ".User::DB_TJGROUP." ug ON u.".User::DB_ID." = ug.".User::DB_TJGROUP_IDUSER."  WHERE ug.".User::DB_TF_ACTIVE." = 1 AND u.".User::DB_TF_ACTIVE." = 1 AND ug.idgroup = ".$idGroup;
		$res = sendsql($q);
		
		for($i = 0; $i < count($res); $i++){
			$user = new User();
			$user->putEmail($res[$i]['email']);
			$user->putId($res[$i]['iduser']);
			$user->putHumanName($res[$i]['humanname']);
			$user->putOther('go',$res[$i]['go']);
			
			$this->users[] = $user;
			unset($user);
		}	
		
	}
	
	public function loadComments(){
		$q = "SELECT * FROM ".self::DB_COMMENT." WHERE ".self::DB_COMMENT_IDGROUP." = ".$this->id;
		$res = sendsql($q);
		
		$this->comments = array();
		for($i = 0; $i < count($res); $i++){
			$comment = new Comment($res[0][self::DB_COMMENT_IDGROUP], $res[0][self::DB_COMMENT_AUTOR], $res[0][self::DB_COMMENT_COMMENT], $res[0][self::DB_COMMENT_IDPLACE]);
			$comment->putId($res[0][self::DB_COMMENT_IDCOMMENT]);
			$comment->putLastUpdate($res[0][self::DB_COMMENT]);
			$this->comments[] = $comment;
			unset($comment);
			
		}
	}
	
	public function loadPlaces(){
		$q = "SELECT ".User::DB_TPGROUP_IDPLACE." FROM ".User::DB_TPGROUP." WHERE ".self::DB_COMMENT_IDGROUP." = ".$this->id;
		$res = sendsql($q);
		
		$this->places = array();
		for($i = 0; $i < count($res); $i++){
			$this->places[] = $res[$i][User::DB_TPGROUP_IDPLACE];
		}			
		
	}
	
	public function tojson(){
		$result = array(
			idOnline => $this->id,
			name => $this->name,
			owner => $this->owner->getId(),
			lastUpdate => $this->lastupdate
		);
		
		$result['users'] = array();	
		if(!is_null($this->users)){
			for($i = 0;  $i < count($this->users); $i++){
				$result['users'][] = $this->users[$i]->getId();				
			}
		}
		
		$result['comments'] = array();
		for($i = 0; $i < count($this->comments); $i++){
				$result['comments'][] = $this->comments[$i]->tojson();				
		}
		
		$result['places'] = $this->places;
			
		return $result;
	}
}

?>
